package donjuan;
//该类用于对数据库进行操作以及对储存的动态对象数据进行操作
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Readin {
  public static String name = "";
  
  public static boolean status = false;
  
  private static int numb = 2;//最大容纳量
  
  private static int cap = 0;//当前数据个数
  
  protected static int count = 1;//记录对象数据个数
  
  public String[] data = new String[5];
  
  Readin() {}
  
  Readin(String[] datum) {//在后续添加新数据时会使用该构造方法
    cap++;
    for (int i = 0; i < 5; i++)
      this.data[i] = datum[i]; 
  }
  
  Readin(ResultSet rs) throws SQLException 
  {//此方法在程序启动时使用，只会使用一次
    cap++;
    count++;
    String muda = rs.getString(1);
    for (int i = 1; i < 6; i++)
    {
    	this.data[i - 1] = rs.getString(i + 1);
      if(this.data[i-1]==null)//防止用户什么都不输,且该数据表为null;一旦出现null并且不处理，程序就会在打表格的时候崩溃
    		this.data[i-1]=" ";
    }
  }
  //该方法负责展示在删除数据时弹出窗口，询问用户是否需要删除，以进行后续响应
  private static ArrayList<Integer> decide_data(ArrayList<Readin> cha, ArrayList<Integer> pick) {
    boolean judge = true;
    Boolean[] del = new Boolean[pick.size()];
    for (int i = 0; i < pick.size(); ) {
      del[i] = Boolean.valueOf(false);
      i++;
    } 
    int mem = 0;
    for (int j = 0; j < pick.size(); j++) {
      j++;
      JFrame useless = new JFrame();
      useless.setVisible(false);
      int n = JOptionPane.showConfirmDialog(useless, "是否删除第"+ j + "行信息", "删除", 0);
      j--;
      if (n == 0) {
        judge = true;
      } else {
        judge = false;
      } 
      if (judge) {
        del[j] = Boolean.valueOf(true);
        mem++;
      } 
      useless.dispose();
    } 
    ArrayList<Integer> choose = new ArrayList<>(mem);
    for (int k = 0; k < pick.size(); k++) {
      if (del[k].booleanValue())
        choose.add(pick.get(k)); 
    } 
    pick.removeAll(pick);
    return choose;
  }
  public static void resetdata()
  {
	numb=2;
	cap=0;
	count=1;
	status = false;
  }
  public static ArrayList<Integer> search_data(ArrayList<Readin> cha, String sou, Boolean mode) 
  {
	int num = 2;  //寻找用户输入数据，依据mode来决定是否需要精准查找
    int capa = 0;
    ArrayList<Integer> line = new ArrayList<>(2);
    if(!sou.equals("all"))
    {
    	
       for (int i = 0; i < cha.size(); i++) 
	   {
		   String[] arrayOfString = cha.get(i).data;
		   for (int j=0;j<5;j++) 
		      {
		        String seek = arrayOfString[j];
		        if (seek != null) 
		        {
		          if (capa >= num) //扩容用
		          {
		            num += 2;
		            cha.ensureCapacity(num);
		          } 
		          if (mode) //mode为真是精准查找
		          {
		            if (seek.equals(sou))
		            {
		            	 line.add(i); 
		            	 break;
		            }
		           
		          } 
		          else 
		          {
		        	 if (seek.contains(sou)) 
		        	 {
		        		 line.add(i);
			        	 break;
		        	 }
		          }
		        } 
		      } 
	    } 
    }
    else for (int i = 0; i < cha.size(); i++) line.add(i); 
    return line;
  }
  //向数据库末尾和对象动态数组末尾添加数据
  public static void add_data(ArrayList<Readin> cha, String[] datum, Statement sql) {
    judge(cha);
    cha.add(new Readin(datum));
    String jue = "insert into " + name + " values" + "('" + count + "',";
    for (int i = 0; i < 4; ) {
      jue = String.valueOf(jue) + "'" + datum[i] + "',";
      i++;
    } 
    jue = String.valueOf(jue) + "'" + datum[4] + "')";
    try {
      sql.executeUpdate(jue);
    } catch (SQLException e) {
      e.printStackTrace();
    } 
    count++;
  }
//向数据库和对象动态数组中删除指定数据
  public static int del_data(ArrayList<Readin> cha, String sou, Statement sql, Connection con, boolean mode) {
    ArrayList<Integer> del = decide_data(cha, search_data(cha, sou, Boolean.valueOf(mode)));
    if (del.size() == 0)
      return -1; 
    for (Iterator<Integer> iterator1 = del.iterator(); iterator1.hasNext(); ) {
      int i = ((Integer)iterator1.next()).intValue();
      i++;
      String jue = "DELETE FROM " + name + " WHERE Id=" + i + ";";
      try {
        sql.executeUpdate(jue);
      } catch (SQLException e) {
        System.exit(-1);
      } 
    } 
    int p = 0;
    for (Iterator<Integer> iterator2 = del.iterator(); iterator2.hasNext(); ) {
      int i = ((Integer)iterator2.next()).intValue();
      i -= p;
      cha.remove(i);
      p++;
    } 
    cap -= del.size();
    count -= del.size();
    del.removeAll(del);
    reset_id(cha, con);//只有在随机删除时才会使用到复位id编号的方法
    return 1;
  }
//复位id编号以实现递增
  private static void reset_id(ArrayList<Readin> cha, Connection con) {
    PreparedStatement ps = null;
    int reset = 1;
    String comm = "";
    for (Readin change : cha) 
    {
      comm = "UPDATE " + name + " SET Id = ? WHERE Note = ? AND Account = ?";
      try {
        ps = con.prepareStatement(comm);
        ps.setInt(1, reset);
      //  ps.setInt(1, String.valueOf(reset));
        ps.setString(2, change.data[4]);
        ps.setString(3, change.data[0]);
        ps.executeUpdate();
      } catch (SQLException e1) { System.exit(-1);}
      reset++;
    } 
  }
  //刷新指定数据行
  public static void refresh_data(ArrayList<Readin> cha, String[] sou, Connection con, int location) 
  {
    String[] menu = { "Account", "Password", "Website", "Information", "Note" };
    String jue = "";
    PreparedStatement ps = null;
    location++;
    for (int i = 0; i < 5; i++) {
      if (sou[i] != null)
        cha.get(location - 1).data[i] = sou[i]; 
      jue = "UPDATE " + name + " SET " + menu[i] + "=? WHERE Id =?";
      try 
      {
        ps = con.prepareStatement(jue);
        ps.setString(1, cha.get(location - 1).data[i]);
        ps.setString(2, String.valueOf(location));
        ps.executeUpdate();
      } catch (SQLException e1) {
        System.exit(-1);
      } 
    } 
    try {
      ps.close();
    } catch (SQLException e) {
      System.exit(-1);
    } 
  }
  //判断对象动态数组是否越界
  public static void judge(ArrayList<Readin> cha) {
    if (cap >= numb) {
      numb += 2;
      cha.ensureCapacity(numb);
    } 
  }
  //控制台数据输出用，调试阶段使用，代码实际运行时不会使用到
  public String toString() {
    String re = "";
    byte b;
    int i;
    String[] arrayOfString;
    for (i = (arrayOfString = this.data).length, b = 0; b < i; ) {
      String da = arrayOfString[b];
      re = String.valueOf(re) + " " + da;
      b++;
    } 
    return re;
  }
}
